/*
 * @Author: zhongPlee info@witop.cn
 * @Date: 2023-12-23 09:35:31
 * @LastEditors: zss 'zss872406501@163.com'
 * @LastEditTime: 2024-03-26 10:15:50
 * @FilePath: \helpDocs\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

// 最后点击的id
let lastClickedId = ''
// 多对一的a标签
let arr = ['#global', '#association', '#QBEsearch', '#anniu', '#event', '#edit', '#assTable', '#group', '#DetailFE', "#table", "#form", "#view", "#tab", "#quote", "#mform", "#tree", "#date", "#doc", "#cavans", "#refcavans", "#import", "#export", "#relationship", "#slc_field", "#tempConfig", "#add_permission", "#autoLoad"];

//文本跳转闪烁

const allAEl = document.querySelectorAll('.markdown-preview a')

for (let i = 0; i < allAEl.length; i++) {
    allAEl[i].onclick = function () {
        if (arr.includes(`#${this.id}`)) {
            lastClickedId && (allAEl[i].href = `#${lastClickedId}`);
        }
        if (this.id) {
            lastClickedId = this.id
        }
        const hrefEl = document.querySelector(`#${allAEl[i].hash.substring(1)}`)
        hrefEl.classList.add('blinking-text')
        setTimeout(() => {
            hrefEl.classList.remove('blinking-text')
        }, 1000)
    }
}



// const back = function (el) {
//     const a = getElement(el)
//     a.onclick = function () {
//         lastClickedId && (a.href = `#${lastClickedId}`);
//     }
// }

const getElement = function (el) {
    return document.querySelector(el)
}

// arr.forEach(item => {
//     back(item)
// })

// 懒加载
let imgs = document.querySelectorAll('img')
imgs.forEach(item => {
    item['data-src'] = item.src;
    item.src = '';
})

function lozyLoad() {
    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    let winHeight = window.innerHeight;
    for (let i = 0; i < imgs.length; i++) {
        if (imgs[i].offsetTop <= scrollTop + winHeight) {
            imgs[i].src = imgs[i]['data-src'];
        }
    }
}
lozyLoad()
window.addEventListener('scroll', lozyLoad)

// 添加按钮
let parent = getElement('.md-sidebar-toc');
parent.insertAdjacentHTML('afterbegin', `<div class="topBox">
          <div class="iconBox"><button class="expand" title="收起"><i class="wy-icon wy-wenjianjia-4"></i></button><button class="accept" title="展开"><i class="wy-icon wy-wenjianjia2"></i></button></div>
          <div class="search"><input type="text" placeholder="search"><ul class="drwnBox"></ul></div></div>`)




setTimeout(() => {
    // 默认打开侧边栏
    sidebarTOCBtn.click()
}, 500);



const expandBtn = getElement('.expand');
const acceptBtn = getElement('.accept');
let navs = getElement('body > div.md-sidebar-toc > div.md-toc').children
// 收起
expandBtn.onclick = function () {
    for (let index = 0; index < navs.length; index++) {
        const element = navs[index];
        if (element.hasAttribute('open')) {
            element.removeAttribute('open')
        }
        // element.removeAttribute('open')
    }
}

// 默认收起
expandBtn.click();

// 展开
acceptBtn.onclick = function () {
    for (let index = 0; index < navs.length; index++) {
        const element = navs[index];
        if (!element.hasAttribute('open')) {
            element.setAttribute('open', true)
        }
    }
}


// 搜索
const search = getElement("div.search > input[type=text]")
// 下拉框
const drwnBox = getElement('.drwnBox')
// 数据
const all = document.querySelectorAll('.md-toc p')
// 下拉数据
const drwnData = []

all.forEach(item => {
    drwnData.push(item.innerText)
})


search.oninput = debounce((e) => {
    let value = e.target.value
    if (value) {
        drwnBox.style.display = 'block'
        const data = new Set()
        drwnData.forEach(item => {
            if (item.indexOf(value) !== -1) {
                // console.log(11);
                let li = document.createElement('li')
                let a = document.createElement('a')
                let val = item.replace(/[A-Z]+/g, c => {
                    return c.toLowerCase()
                })
                a.href = `#${val}`
                a.innerHTML = item
                li.appendChild(a)
                data.add(li)
            }
        })
        if (data.size !== 0) {
            for (const li of data) {
                drwnBox.appendChild(li)
            }
            const navs = drwnBox.querySelectorAll('a')
            navs.forEach(item => {
                item.addEventListener('click', () => {
                    drwnBox.style.display = 'none';
                    e.target.value = item.innerText;
                })
            })
        }
        data.clear()
    } else {
        drwnBox.style.display = 'none'
        drwnBox.innerHTML = ''
    }
})

// 防抖
function debounce(fn, wait = 300) {
    let timeout = null;
    return function (e) {
        if (timeout !== null) clearTimeout(timeout);
        timeout = setTimeout(() => {
            fn.apply(this, arguments)
        }, wait);
    }
}

document.title = '使用文档';


